/*
 * ChooseLocalPicturePresenter.java
 * business
 *
 * Created by ChenTao on 2017/5/16.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.chooselocalpicture;

import com.yidiandao.business.data.bean.FileTraversal;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseLocalPictureGridItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseLocalPicturePopListItem;
import com.yidiandao.business.utils.LocalPictureUtil;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/** 本地图片多选 */
public class ChooseLocalPicturePresenter implements ChooseLocalPictureContract.Presenter {
    final ArrayList<DisplayListItem> displayListItems = new ArrayList<>();
    private ChooseLocalPictureContract.View mView;
    private List<FileTraversal> mLocallist;

    public ChooseLocalPicturePresenter(ChooseLocalPictureContract.View view) {

        mView = view;
        mView.setPresenter(this);
    }

    @Override
    public void start() {
        getAllLocalPicData();
    }

    public void getAllLocalPicData() {
        //用于存储文件夹名  文件夹内图片的张数  最后一张图片
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        LocalPictureUtil util = new LocalPictureUtil(UIUtils.getContext());
                        //读取本地图片文件夹的信息
                        mLocallist = util.LocalImgFileList();

                        mView.getCurrentActivity()
                                .runOnUiThread(
                                        new Runnable() {
                                            @Override
                                            public void run() {
                                                mView.refreshContent(
                                                        mView.checkResultObject(
                                                                mLocallist));
                                                getIndexData(0);
                                            }
                                        });
                    }
                })
                .start();
    }

    @Override
    public List getLocalList() {
        return mLocallist;
    }

    @Override
    public void getIndexData(int index) {
        displayListItems.clear();
        if (mLocallist != null && mLocallist.size() != 0) {
            for (int i = 0; i < mLocallist.size(); i++) {
                FileTraversal contentData = mLocallist.get(i);
                contentData.isSelected = (i == index ? 1 : 0);

                displayListItems.add(new ChooseLocalPicturePopListItem(contentData));
            }
        }

        mView.setDisPlayItems(displayListItems);

        FileTraversal contentData = mLocallist.get(index);
        ArrayList<DisplayListItem> gridItem = new ArrayList<>();

        for (int i = 0; i < contentData.filecontent.size(); i++) {
            gridItem.add(new ChooseLocalPictureGridItem(contentData.filecontent.get(i)));
        }

        mView.setGridViewData(gridItem);

        mView.setSelectDirText(contentData.filename);
    }
}
